/*
 * ArrayParameterValidator.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.parameters;

import java.util.Hashtable;

import org.eu4ria.exceptions.IllegalParameterValueException;

public class ArrayParameterValidator extends ParameterValidator {

	private int minlength = 0;

	private int maxlength = Integer.MAX_VALUE;

	private Hashtable<String, Object> stringToObject = new Hashtable<String, Object>();

	public int getMaxlength() {
		return maxlength;
	}

	public void setMaxlength(int maxlength) {
		this.maxlength = maxlength;
	}

	public int getMinlength() {
		return minlength;
	}

	public void setMinlength(int minlength) {
		this.minlength = minlength;
	}

	@Override
	public void acceptsValue(Object value, Class type)
			throws IllegalParameterValueException {
		String s = (String) value;
		int elems = countElems(s);
		if (elems < minlength || elems > maxlength) {
			throw new IllegalParameterValueException(
					"The array must contain from " + minlength + " to "
							+ maxlength + " elements.");
		}

		String[] rawArray = parseArray(s, elems);
		this.convertToType(s, rawArray, type);

	}

	private void convertToType(String s, String[] rawArray, Class type)
			throws IllegalParameterValueException {
		if (type.equals(String[].class)) {
			stringToObject.put(s, rawArray);

		} else if (type.equals(int[].class) || type.equals(Integer[].class)) {
			int[] intArray = new int[rawArray.length];
			for (int i = 0; i < intArray.length; i++) {
				try {
					intArray[i] = Integer.parseInt(rawArray[i]);
				} catch (NumberFormatException e) {
					throw new IllegalParameterValueException(
							"Could not parse input to int array.");
				}
			}
			stringToObject.put(s, intArray);

		} else if (type.equals(long[].class) || type.equals(Long[].class)) {
			long[] longArray = new long[rawArray.length];
			for (int i = 0; i < longArray.length; i++) {
				try {
					longArray[i] = Long.parseLong(rawArray[i]);
				} catch (NumberFormatException e) {
					throw new IllegalParameterValueException(
							"Could not parse input to long array.");
				}
			}
			stringToObject.put(s, longArray);

		} else if (type.equals(float[].class) || type.equals(Float[].class)) {
			float[] floatArray = new float[rawArray.length];
			for (int i = 0; i < floatArray.length; i++) {
				try {
					floatArray[i] = Float.parseFloat(rawArray[i]);
				} catch (NumberFormatException e) {
					throw new IllegalParameterValueException(
							"Could not parse input to float array.");
				}
			}
			stringToObject.put(s, floatArray);

		} else if (type.equals(double[].class) || type.equals(Double[].class)) {
			double[] doubleArray = new double[rawArray.length];
			for (int i = 0; i < doubleArray.length; i++) {
				try {
					doubleArray[i] = Double.parseDouble(rawArray[i]);
				} catch (NumberFormatException e) {
					throw new IllegalParameterValueException(
							"Could not parse input to double array.");
				}
			}
			stringToObject.put(s, doubleArray);

		} else if (type.equals(boolean[].class) || type.equals(Boolean[].class)) {
			boolean[] booleanArray = new boolean[rawArray.length];
			for (int i = 0; i < booleanArray.length; i++) {
				booleanArray[i] = Boolean.parseBoolean(rawArray[i]);
			}
			stringToObject.put(s, booleanArray);

		}

	}

	private int countElems(String s) {
		int elems = 0;
		int index = 0;
		int length = s.length();
		int elemCloseLength = "</a.i>".length();
		while ((index != -1) && (index < length)) {
			index = s.indexOf("</a.i>", index);
			if (index != -1) {
				elems++;
				index += elemCloseLength;
			}

		}
		return elems;
	}

	@Override
	public Object getJavaValue(String string) {
		Object o = stringToObject.get(string);
		stringToObject.remove(string);
		return o;
	}

	private String[] parseArray(String xml, int length)
			throws IllegalParameterValueException {
		int arrayStart = xml.indexOf("<arr>");
		int arrayEnd = xml.indexOf("</arr>");
		if ((arrayStart == -1) || (arrayEnd == -1)) {
			throw new IllegalParameterValueException(
					"Could not parse an array from the input string.");
		}
		String body = xml.substring(arrayStart + "<arr>".length(), arrayEnd);
		String[] elems = new String[length];

		int elemStartLength = "<a.i>".length();
		int elemEndLength = "</a.i>".length();
		int i = 0;
		int startIndex = 0;
		int endIndex = 0;
		while(i<length&&startIndex!=-1){
			startIndex = body.indexOf("<a.i>");
			int elemStart = startIndex + elemStartLength;
			endIndex = body.indexOf("</a.i>");
			if(elemStart>endIndex){
				throw new IllegalParameterValueException(
				"Could not parse an array from the input string.");
			}
			elems[i] = body.substring(elemStart, endIndex);
			body = body.substring(endIndex + elemEndLength);
			i++;
		}
		return elems;
	}

}
